/*
* @Author: Choisaaaa
* @Date: 2018/5/15  10:17
*/

import React from "react";
import {Link} from "react-router-dom";

import "./headline.scss";
import Footer from "component/footer/footer.jsx";
import PageTitle from 'component/page-title/page-title.jsx';
import Pagination from 'util/pagination/pagination.jsx';
import TableList from 'util/table-list/table-list.jsx';
import MUtil from 'util/mm.jsx'
import HeadLine from 'service/head-line-service.jsx'

const _mm = new MUtil();
const _headLine = new HeadLine();

class UserList extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            pageNum : 1,        //分页的启始页
            list : [],          //这里需要定义list为数据，否则后面无法使用map函数
            listType : "list"
        };
    }
    componentDidMount(){
        this.loadHeadLineList();
    }
    loadHeadLineList() {
        let listParam = {
            listType : this.state.listType,
            pageNum : this.state.pageNum
        };
        if(this.state.listType === "search"){
            listParam.searchType = this.state.searchType;
            listParam.keyword = this.state.searchKeyWord;
        }
        _headLine.getHeadLineList(listParam).then(res =>{
            this.setState(res);
            this.setState({
                searchKeyWord : ""
            });
        },err => {
            this.setState({
                list: []
            });
            _mm.errorTips(err)
        });

    }
    onPageNumChange(pageNum){//更新页数
        this.setState({
            pageNum : pageNum
        },() => {
            this.getHeadLineList();//重新渲染
        });
    }

    onUpdatePriority(id,oldPriority){
        let newPriority = window.prompt("请输入新的权重值",oldPriority) ;
        console.log(newPriority);
        if(!newPriority){
            return;
        }
        newPriority = parseInt(newPriority);
        if(newPriority == oldPriority){
            return;
        }else{
            let headLine = {
                lineId : id,
                priority : newPriority,
            };
            _headLine.saveHeadLine(headLine).then((res)=>{
                _mm.successTips(res.msg)
                this.loadHeadLineList();
            },(errMsg)=>{
                _mm.errorTips(errMsg)
            });
        }
    }
    onUpdateStatus(id,oldStatus){
        let newStatus = oldStatus == 0 ? 1 :0;
        let tipMsg = oldStatus == 0 ?"确定将该公告信息重新发布吗?" :"确定下架该公告信息吗？";
        if(window.confirm(tipMsg)){
            let headLine = {
                lineId : id,
                enableStatus : newStatus
            };
            _headLine.saveHeadLine(headLine).then((res)=>{
                _mm.successTips(res.msg)
                this.loadHeadLineList();
            },(errMsg)=>{
                _mm.errorTips(errMsg)
            });
        }
    }
    render(){
        let tableHeader = [
            {name : "公告ID" ,width : "10%"},
            {name : "描述" ,width : "15%"},
            {name : "图片" ,width : "10%"},
            {name : "状态" ,width : "10%"},
            {name : "权重" ,width : "10%"},
            {name : "发布时间" ,width : "20%"},
            {name : "已经发布（天）" ,width : "10%"},
            {name : "操作" ,width : "25%"},
        ];
        let ListBody = this.state.list.map((headLine,index)=>{
            return(
                <tr key={index}>
                    <td>{headLine.lineId}</td>
                    <td>{headLine.lineName}</td>
                    <td>
                        <img src={`${headLine.imageHost}${headLine.lineLink}`} alt={headLine.lineName}/>
                    </td>
                    <td>{headLine.statusDes}</td>
                    <td>{headLine.priority}</td>
                    <td>{headLine.createTime}</td>
                    <td>{headLine.totalDays}</td>
                    <td>
                        <a  className="headline-operation" onClick={(e) => this.onUpdatePriority(headLine.lineId,headLine.priority) }>
                            <button type="button" className="btn btn-primary"> 修改权重</button>
                        </a>
                        <a  className="headline-operation" onClick={(e) => this.onUpdateStatus(headLine.lineId,headLine.enableStatus) }>
                            <button type="button" className="btn btn-warning"> {headLine.enableStatus ==0 ? "恢复" : "下架"}</button>
                        </a>
                    </td>
                </tr>
            )
        });
        return (
            <div id="page-wrapper">
                <PageTitle title="公告管理">
                    <div className="page-header-right">
                        <Link to="/headLine/add" className="btn btn-primary btn-lg">
                            <i className="fa fa-plus"/>
                            <span> 发布公告</span>
                        </Link>
                    </div>
                </PageTitle>
                <TableList tableHeads={tableHeader}>
                    {ListBody}
                </TableList>

                <Pagination current={this.state.pageNum}
                            total={this.state.total}
                            onChange={(pageNum) => this.onPageNumChange(pageNum)}/>
                <Footer/>
            </div>
        );
    }
}

export default UserList;